﻿

import Request from '../../base/request.js?t=202511041006'
import Storage from '../../base/storage.js?t=202511041006'
import { imageBaseUrl, isImageFile, getBrowserInfo} from "../../base/utils.js?t=202511041006"
const Composition = (proxy) => {
    const data = reactive({
        pdfOpen: false,
        pdfurl: "",

        proxy: "",
        type: "",
        fileOpen: false,
        subform: {

        },
        logList:[],
        mid: "",
        listInfo: {

        },
        dict: {
            0: '通用表考评',
            1: '重点工作考评',
            2: '现场检查考评',
        },
        s3Form: {
            TeamId: "",
            StartTime: "",
            EndTime: "",
            DepartmentId: "",
            Factor: "",
        },
        s4Form: {
            TeamId: "",
            StartTime: "",
            EndTime: "",
            DepartmentId: "",
            Factor: "",
        },
        checkGroup: [],
        memberGroup: [],

        cmemberGroup1: [],
        cmemberGroup2: [],
        checkFrom: {
            userName: "",
            userId: "",
            checkDate: "",
            advice: ""
        },
        previewShow: false,
        imgUrl: "",
        form: {
            plan: null,
            year: "",
            person: "",
            duty: "",
            subdept: ""

        },
        totala: 0,
        totals: 0,
        TotalScore: 0,
        ti: 0,
        i: 0,
        is: 0,
        iss: 0,
        planList: [],

        //lists
        lists: [

        ],
        activeKey: [0, 1],
        messageTest: "",
        formState: {
            dept: "",
            name: "",
            job: "",
            role: "",
            tel: "",
        },
        chooseFiles: [],
        deptList: [
            { label: '公司办公室', value: '0' },
            { label: '发展规划部', value: '1' },
            { label: '财务部', value: '2' },
            { label: '人力资源部', value: '3' },
            { label: '审计部', value: '4' },
            { label: '法律部', value: '5' },
            { label: '党委工作部', value: '6' },
            { label: '监督检查办公室', value: '7' },
            { label: '工会', value: '8' },
            { label: '团委', value: '9' },
            { label: '项目管理部', value: '10' },
            { label: '生产管理部', value: '11' },
            { label: '科技管理部', value: '12' },
            { label: '制造工程技术中心', value: '13' },
            { label: '采供物流中心', value: '14' },
            { label: '质量管控中心', value: '15' },
            { label: '计量测试中心', value: '16' },
        ],
        monthList: [
            { label: '1月', value: '1' },
            { label: '2月', value: '2' },
            { label: '3月', value: '3' },
            { label: '4月', value: '4' },
            { label: '5月', value: '5' },
            { label: '6月', value: '6' },
            { label: '7月', value: '7' },
            { label: '8月', value: '8' },
            { label: '9月', value: '9' },
            { label: '10月', value: '10' },
            { label: '11月', value: '11' },
            { label: '12月', value: '12' },
        ],

        pagination: {
            total: 0,
            loading: true,
            current: 1,
            pageSize: 10,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 10,
            // pageSizeOptions: ['5', '10', '15', '20'], // 可不设置使用默认
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗
        },


        memberList: [],

        chooseMember: {},


        rules: {
            dept: [{ required: true, message: '请选择部门', trigger: 'change' }],
            name: [{ required: true, message: '请选择成员', trigger: 'change' }],

        },
        opType: "",
        confirmShow: false,
        delId: "",

        open: false,

        queryParams: {
            role: "",
            dept: "",
        },
        statusList: [
            { name: '组长', code: '0' },
            { name: '组员', code: '1' },

        ],


        columns: [
            {
                title: '序号',
                dataIndex: 'index',
                key: 'index',
                align: 'center',
                width: 80,
                customRender: ({ index}) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
                },
            },
            {
                title: '应用模块',
                dataIndex: 'a',
                key: 'a',
                align: 'center',
                customRender: function ({ text, record, index, column }) {
                    let linkBtn = h(antd.Button, { type: 'link' }, () => text)


                    return linkBtn
                }


            },
            {
                title: '考评扣分',
                dataIndex: 'b',
                key: 'b',
                align: 'center',
                customRender: function ({ text, record, index, column }) {

                    let numberInput = h(antd.InputNumber, { style: 'width:140px', placeholder: "请输入考评扣分", min: 0, max: 100 }, () => text)


                    return numberInput
                }
            },
            {
                title: '扣分缘由',
                dataIndex: 'c',
                key: 'c',
                align: 'center',
                customRender: function ({ text, record, index, column }) {
                    let textArea = h(antd.Textarea, { placeholder: "请输入扣分缘由", autoSize: { minRows: 2, maxRows: 5 } }, () => text)
                    return textArea
                }
            },

        ],
        dataSource: [],
        fileList: [],

   



        itemRender: (data) => {










            let uid = data.file.uid
            const text = h('div', { style: 'cursor:pointer;margin-left:10px' }, data.file.name)
            const previewBtn = h('img', { style: 'cursor:pointer;margin:0 4px;', onclick: () => { methods.btnPreview(uid, 1) }, src: "../Bussiness/img/svg/preview.svg", width: 20, height: 22 }, '预览')
            const downloadBtn = h('img', { style: 'cursor:pointer;margin:0 4px;', onclick: () => { methods.btnDownLoad(uid, 1) }, src: "../Bussiness/img/svg/download.svg", width: 20, height: 22 }, '下载')
            const deleteBtn = h('img', { style: 'cursor:pointer;margin:0 4px;', onclick: () => { methods.btnDelete(uid, 1) }, src: "../Bussiness/img/svg/delete.svg", width: 18, height: 22 }, '删除')
            var btns = ""
            if (isImageFile(data.file.url)) {
                btns = h('div', { style: "display:flex;margin-left:10px" }, [
                    previewBtn,
                    downloadBtn,
                    //deleteBtn
                ])
            } else {
                btns = h('div', { style: "display:flex;margin-left:10px" }, [
                    downloadBtn,
                    //deleteBtn
                ])
            }

            return h('div', { style: 'margin-top:10px;align-items:center; width:100%;display:flex;justify-content:space-between' }, [
                text, btns
            ])

        },
    })
    let params = toRefs(data)

    const methods = {
        filterOption: (val, type) => {
            return `${type.value}`.includes(`${val || ''}`)
        },
        handleChange: (info) => {
            let formData = new FormData();
            formData.append('nfile', info.file.originFileObj)
            formData.append('uid', info.file.originFileObj.uid)
            Request({
                url: "/download/uploadfile?Path=Performance",
                method: 'post',
                data: formData
            }).then(res => {
                let item = params.lists.value[params.ti.value].NodeList
                [params.i.value].NodeList[params.is.value].NodeList
                [params.iss.value].ResultItemFileList.find(e => e.uid == res.Data[0].uid)
                item.url = imageBaseUrl + res.Data[0].FilePath.split('../..')[1]
                item.FileId = res.Data[0].FileId
                item.FileName = res.Data[0].FileName
            })

        },
        //handleChange1: (info) => {
        //    let formData = new FormData();
        //    formData.append('nfile', info.file.originFileObj)
        //    formData.append('uid', info.file.originFileObj.uid)
        //    Request({
        //        url: "/download/uploadfile?Path=Performance",
        //        method: 'post',
        //        data: formData
        //    }).then(res => {
        //        let item = params.fileList.value.find(e => e.uid == res.Data[0].uid)
        //        item.url = imageBaseUrl + res.Data[0].FilePath.split('../..')[1]
        //        item.FileId = res.Data[0].FileId
        //    })

        //},
        downloadfun: (name, url) => {
            // 创建a标签
            let a = document.createElement('a')
            // 定义下载名称
            a.download = name
            // 隐藏标签
            a.style.display = 'none'
            // 设置文件路径
            a.href = url
            // 将创建的标签插入dom
            document.body.appendChild(a)
            // 点击标签，执行下载
            a.click()
            // 将标签从dom移除
            document.body.removeChild(a)
        },
        setVisible: (value) => {
            params.previewShow.value = value;
        },
        btnPreview: (e, type) => {
            if (type) {
                let item = params.fileList.value.find(es => es.uid == e)
                item.url = item.FilePath || item.url
                if (item.url.indexOf('.pdf') != -1) {
                    params.pdfurl.value = item.url
                    params.pdfOpen.value = true
                } else {
                    params.imgUrl.value = item.url

                    methods.setVisible(true)
                }
                return
            }

            let item = params.chooseFiles.value.find(es => es.uid == e)
            item.url = item.FilePath || item.url
            if (item.url.indexOf('.pdf') != -1) {
                params.pdfurl.value = item.url
                params.pdfOpen.value = true
            } else {
                params.imgUrl.value = item.url

                methods.setVisible(true)
            }
        },

        btnDownLoad: (e, type) => {
            if (type) {

                let item = params.fileList.value.find(es => es.uid == e)
                item.url = item.FilePath || item.url
                const { name, url } = item
                methods.downloadfun(name, url)
                return
            }

            let item = params.chooseFiles.value.find(es => es.uid == e)
            item.url = item.FilePath || item.url
            const { name, url } = item
            methods.downloadfun(name, url)

        },
        btnDelete: (e, type) => {
            if (type) {

                params.chooseFiles.value = params.chooseFiles.value.filter(es => es.uid != e)

                params.fileList.value = params.fileList.value.filter(es => es.uid != e)

                return
            }

            params.lists.value[params.ti.value].NodeList
            [params.i.value].NodeList[params.is.value].NodeList
            [params.iss.value].ResultItemFileList = params.lists.value[params.ti.value].NodeList
            [params.i.value].NodeList[params.is.value].NodeList
            [params.iss.value].ResultItemFileList.filter(es => es.uid != e)
            params.chooseFiles.value = params.chooseFiles.value.filter(es => es.uid != e)
        },

        planChange: (e) => {

            Request({
                url: "/Performance/GetSelfTempleteByPlan",
                method: 'post',
                params: {
                    Id: e
                }

            }).then(res => {

                params.list.value = res.Data[0].NodeList
                params.TotalScore.value = res.Data[0].TotalScore

            })
        },
        handleFile: (i, is, iss) => {
            params.fileOpen.value = true


            params.chooseFiles.value = params.lists.value[params.ti.value].NodeList
            [params.i.value].NodeList[params.is.value].NodeList
            [params.iss.value].ItemFileList.map(e => {
                e.url = e.url ? e.url : imageBaseUrl + e.FilePath.split('../..')[1]
                e.FileId = e.FileId
                e.name = e.FileName
                e.isDelete = false
                e.isImage = isImageFile(e.url)
                return {
                    ...e,

                    isImage: isImageFile(e.url)
                }
            })
            console.log(params.chooseFiles.value,'  params.chooseFiles.value ')

             

        },
        handleFile1: (ti, i, is, iss) => {
            params.fileOpen.value = true

      

            params.chooseFiles.value = params.lists.value[params.ti.value].NodeList
            [params.i.value].NodeList[params.is.value].NodeList
            [params.iss.value].ResultItemFileList.map(e => {
                e.url = e.url ? e.url : imageBaseUrl + e.FilePath.split('../..')[1]
                e.FileId = e.FileId
                e.name = e.FileName
                e.isDelete = false
                e.isImage = isImageFile(e.url)
                return {
                    ...e,

                    isImage: isImageFile(e.url)
                }
            })
            console.log(params.chooseFiles.value, '   params.chooseFiles.value ')

        },
        iichange: (i) => {
            let f = params.lists.value[i].NodeList.map((e, i) => {
                let ff = {

                }
                if (e.CalcType == 0) {
                    if (e.NodeList) {
                        let zz = e.NodeList.map(es => {
                            if (es.NodeList) {
                                let res = es.NodeList.map(ess => {
                                    return ess.ResultScore ? ess.ResultScore : 0
                                })
                                return res.reduce((prev, cur, index, arr) => {
                                    return prev + cur
                                }, 0)
                            }
                        })
                        ff.sub = zz.reduce((prev, cur, index, arr) => {
                            return prev + cur
                        }, 0)
                    }
                } else {
                    if (e.NodeList) {
                        let zz = e.NodeList.map(es => {
                            if (es.NodeList) {
                                let res = es.NodeList.map(ess => {
                                    return ess.ResultScore ? ess.ResultScore : 0
                                })
                                return res.reduce((prev, cur, index, arr) => {
                                    return prev + cur
                                }, 0)
                            }
                        })
                        ff.add = zz.reduce((prev, cur, index, arr) => {
                            return prev + cur
                        }, 0)
                    }
                }

                return ff
            })


            params.lists.value[i].totala = 0
            params.lists.value[i].totals = 0
 

            
            for (let e of f) {
                if (e.add) {
                    params.lists.value[i].totala += e.add
                } else if (e.sub) { params.lists.value[i].totals += e.sub }
            }


         

        },
        ichange: () => {


            let f = params.lists.value[0].NodeList.map((e, i) => {
                let ff = {

                }
                if (e.CalcType == 0) {
                    if (e.NodeList) {
                        let zz = e.NodeList.map(es => {
                            if (es.NodeList) {
                                let res = es.NodeList.map(ess => {
                                    return ess.SelfScore ? ess.SelfScore : 0
                                })
                                return res.reduce((prev, cur, index, arr) => {
                                    return prev + cur
                                }, 0)
                            }
                        })
                        ff.sub = zz.reduce((prev, cur, index, arr) => {
                            return prev + cur
                        }, 0)
                    }
                } else {
                    if (e.NodeList) {
                        let zz = e.NodeList.map(es => {
                            if (es.NodeList) {
                                let res = es.NodeList.map(ess => {
                                    return ess.SelfScore ? ess.SelfScore : 0
                                })
                                return res.reduce((prev, cur, index, arr) => {
                                    return prev + cur
                                }, 0)
                            }
                        })
                        ff.add = zz.reduce((prev, cur, index, arr) => {
                            return prev + cur
                        }, 0)
                    }
                }

                return ff
            })
            let totala = 0
            let totals = 0
            for (let e of f) {
                if (e.add) {
                    totala += e.add
                } else if (e.sub) { totals += e.sub }
            }
            params.totala.value = totala
            params.totals.value = totals


        },
        handleDrop: (e) => {
            console.log(e, 'drag');
        },
        getTableList: (isRresh) => {
            if (isRresh) {
                params.pagination.value.current = 1
            }
            //Request({
            //    url: "performance/GetTemplistList",
            //    method: 'post',
            //    params: {
            //        Years: 2024,
            //        TypeId: 1
            //    }
            //}).then(res => {
            //    params.list.value = res.Data[0].NodeList
            //    console.log(res, 'res')

            //})
        },
        upload: (ti, i, is, iss) => {
            params.ti.value = ti
            params.i.value = i
            params.is.value = is
            params.iss.value = iss

            if (!params.lists.value[params.ti.value].NodeList
            [params.i.value].NodeList[params.is.value].NodeList
            [params.iss.value].ResultItemFileList) {
                params.lists.value[params.ti.value].NodeList
                [params.i.value].NodeList[params.is.value].NodeList
                [params.iss.value].ResultItemFileList = []
            }

        },
        init: () => {
              getBrowserInfo().catch(err => { console.log(err)})
            if (proxy) {
                params.proxy.value = proxy
            }
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount: 1,
                rtl: true,
            });
 
            function queryURLParams() {
                const searchParams = window.location.search.substring(1).split("&");
                const params = {};

                for (const param of searchParams) {
                    const [key, value] = param.split("=");
                    params[key] = value;
                }

                return params;
            }
            const { id } = queryURLParams()
            params.mid.value = id

            let info = Storage.get('AccountInfo', true)

            params.subform.value.userName = info.UserName
            params.subform.value.subTime = dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss')
            Request({
                url: "/Account/createJwtToken",
                method: 'post',
                data: {
                    "userName": info && info.UserNo
                }
            }).then(res => {
                Storage.set('token', res.Data,true)
                methods.getTableList(true)

                Request({
                    url: "/performance/GetSelfCheckFormEvaluation",
                    method: 'post',
                    params: {
                        FormId: params.mid.value
                    }
                }).then(ress => {
         
                    params.listInfo.value = ress.Data
                    params.listInfo.value.UpdateTime = dayjs(parseInt(params.listInfo.value.UpdateTime.match(/\/Date\((\d+)\)\//)[1], 10)).format('YYYY-MM-DD')
                    params.checkFrom.value.userName = info.UserName
                    params.checkFrom.value.subTime = dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss')
                    params.fileList.value = ress.Data.FileList.map(e => {
                        return {
                            ...e,
                            name: e.FileName,
                            url:  e.url ? e.url : imageBaseUrl + e.FilePath.split('../..')[1],
                            FileId: e.FileId
                        }
                    })
                    params.lists.value = ress.Data.ItemList.map(e => {
                        return {
                            ...e,
                            totala: 0,
                            totals: 0,
                        }
                    })




                    methods.ichange()
                    methods.iichange(0)
                    methods.iichange(1)
                    methods.iichange(2)


                    Request({
                        url: "/performance/GetOperateLogList",
                        method: 'post',
                        params: {
                            FormId: ress.Data.PlanId
                        }
                    }).then(res => {
                        params.logList.value = res.Data.map(e => {

                            e.OperateTime = dayjs(parseInt(e.OperateTime.match(/\/Date\((\d+)\)\//)[1], 10)).format('YYYY-MM-DD')
                            return e
                        })
                        console.log(res,'res')
                    })
                    

                    Request({
                        url: "/performance/getplanteamlist",
                        method: 'post',
                        params: {
                            FormId: ress.Data.PlanId
                        }
                    }).then(res => {
                        params.checkGroup.value = res.Data.map(e => {
                            return {
                                label: e.TeamName,
                                value: e.Id,
                                list: e.MemberList
                            }
                        })

                        Request({
                            url: "/SystemConfig/GetUserListByDepartment",
                            method: 'post',
                            params: {
                                DepartmentId: ress.Data.DepartmentId
                            }
                        }).then(res => {


                            params.memberGroup.value = res.Data.map(e => {
                                return {
                                    label: `${e.UserName}(${e.UserNo})`,
                                    value: e.UserNo
                                }
                            })
                            let list = ress.Data.EvalutionFormList




                            params.s3Form.value.duty = list[0].DepartmentManager
                            params.s3Form.value.group = list[0].TeamId
                            methods.groupChange(params.s3Form.value.group)
                            if (list[0].EvaluationDate) {
                                params.s3Form.value.Time = dayjs(parseInt(list[0].EvaluationDate.match(/\/Date\((\d+)\)\//)[1], 10)).format('YYYY-MM-DD')
                            }
                        
                            params.s3Form.value.checkMan = list[0].ApproverList&& list[0].ApproverList.split(',')
                            params.s3Form.value.member = list[0].CooperatorList&& list[0].CooperatorList.split(',')



                            params.s4Form.value.duty = list[1].DepartmentManager
                            params.s4Form.value.group = list[1].TeamId
                            methods.groupChange1(params.s4Form.value.group)
                            if (list[1].EvaluationDate) {
                                params.s4Form.value.Time = dayjs(parseInt(list[1].EvaluationDate.match(/\/Date\((\d+)\)\//)[1], 10)).format('YYYY-MM-DD')
                            }
                    
                            params.s4Form.value.checkMan = list[1].ApproverList&& list[1].ApproverList.split(',')
                            params.s4Form.value.member = list[1].CooperatorList&& list[1].CooperatorList.split(',')
                        })


        
                    })

           


     


                })





            })

            params.form.value.person = info.UserName
            params.form.value.subdept = info.DepartmentName




            let arry = ['责任', '目标', '风险', '隐患', '培训', '文化', '制度', '应急', '合规', '相关方']
            params.dataSource.value = arry.map(e => {
                return {
                    a: e,
                    b: "",
                    c: ""
                }
            })
            function queryURLParams() {
                const searchParams = window.location.search.substring(1).split("&");
                const params = {};

                for (const param of searchParams) {
                    const [key, value] = param.split("=");
                    params[key] = value;
                }

                return params;
            }
            const { type } = queryURLParams()
            params.type.value = type

        },
        groupChange: (e) => {



            let item = params.checkGroup.value.find(es => es.value == e)

            if (item && item.list && item.list.length != 0) {
                params.cmemberGroup1.value = item.list.map(e => {
                    return {
                        label: `${e.MemberName}(${e.UserNo})`,
                        value: e.UserNo
                    }
                })
                params.s3Form.value.Time = dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss')
                params.s3Form.value.checkMan = []
            }
         
            //params.s3Form.value.checkMan = item.list.map(es => `${es.MemberName}(${es.UserNo})`).toString()
            //
        },

        groupChange1: (e) => {
            let item = params.checkGroup.value.find(es => es.value == e)



            if (item && item.list && item.list.length != 0) {
                params.cmemberGroup2.value = item.list.map(e => {
                    return {
                        label: `${e.MemberName}(${e.UserNo})`,
                        value: e.UserNo
                    }
                })
                params.s4Form.value.Time = dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss')
                params.s4Form.value.checkMan = []

            }

          
            //params.s4Form.value.checkMan = item.list.map(es => `${es.MemberName}(${es.UserNo})`).toString()
            //
        },

        onSbumit: (Status) => {
            
            Request({
                url: "/performance/ApproveYearReport",
                method: 'post',
                data: {
                    FormId: params.mid.value,
                    IsApprove: Status,
                    Remark: params.subform.value.advice
                }
            }).then(res => {
                params.messageTest.value.success(res.Msg);

                setTimeout(() => {
                    methods.onBack()
                },1500)
            })
        },
        onBack: () => {
            window.location.href = '/Performance/PerformanceMeansure'
        }
    }
    const template = {
        pdfPopModel: {
            template: `
           <a-modal v-model:open="pdfOpen" width = "80%" title = "PDF预览" v-on:ok="pdfOpen=false" >
<div style='height:700px'>
                <embed id="pdfviewscreen" :src="pdfurl" type="application/pdf" width="100%" style="height:100%;" />

</div>
  
            </a-modal >
`,
            setup() {
                return {
                    ...params,
                    ...methods,
                };
            }
        },



        bottomBtns: {
            template: `<div class="w-full bg-[#eeeeee] h-50  flex  items-center justify-end">
                    <a-space>
                        <a-button v-on:click="onSbumit(false)" type="primary" danger>驳回</a-button>
                        <a-button v-on:click="onSbumit(true)" type="primary">通过</a-button>
                        <a-button v-on:click="onBack">返回</a-button>
                    </a-space>
                </div>`,
            setup() {

                return {
                    ...params,
                    ...methods,
                };
            }

        }
    }


    //let template = toRefs(vtemplate)
    return { // 返回对象给模板

        ...params,
        ...methods,
        template

    }
}


export default Composition